package luj.tool.jnproxyan.proxy.stats.http.history;

import luj.tool.jnproxyan.proxy.network.socks.request.RequestBody;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryDat;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryItem;

import java.time.Instant;
import java.util.concurrent.atomic.AtomicLong;

public class HttpHistoryAdder {

  public HttpHistoryAdder(HttpHistoryDat historyDat, Long socksId, RequestBody socksBody, Instant startTime) {
    _historyDat = historyDat;
    _socksId = socksId;
    _socksBody = socksBody;
    _startTime = startTime;
  }

  public void add() {
    AtomicLong nextId = _historyDat.getNextItemId();
    Long itemId = nextId.getAndIncrement();

    String host = _socksBody.getTargetHost();
    int port = _socksBody.getTargetPort();

    var item = new HttpHistoryItem(itemId, _socksId, host, port, _startTime);
    _historyDat.getItemList().add(item);
  }

  private final HttpHistoryDat _historyDat;
  private final Long _socksId;

  private final RequestBody _socksBody;
  private final Instant _startTime;
}
